package com.knife.blog.logic;

import com.knife.web.Globals;
//import com.knife.blog.domain.BlogCircle;
import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.BlogTemplate;
import com.knife.blog.util.BlogFileUtil;
import com.knife.web.tools.EasyDBODAO;
import com.knife.web.tools.IDAO;
import org.apache.log4j.Logger;

public class AutoHtmlGenerater implements java.lang.Runnable {

    private static final Logger logger = (Logger) Logger.getLogger(AutoHtmlGenerater.class.getName());
    private static long autoRetryTime = 1000 * 60 * 1;
    private static IDAO dao = EasyDBODAO.getInstance();
    private boolean isRunning = false;

    public void start() {
        if (!isRunning) {
            Thread thread = new Thread(this);
            thread.start();
        }
    }

    public void run() {
        isRunning = true;
        System.out.println("自动生成HTML的线程已启动！");
        logger.info("自动生成HTML的线程已启动!");
        while (true) {
            try {
                Thread.sleep(autoRetryTime);
               // HTMLGenerater.saveToHTML();
                HTMLGenerater.saveIndexArticleToHTML();
                HTMLGenerater.saveIndexCircleToHTML();
                HTMLGenerater.saveIndexPhotoToHTML();
                //java.util.List circleList = this.dao.query(BlogInfo.class, "type='circleBlog' and updated=1");
                java.util.List list = this.dao.query(BlogInfo.class,
                        "updated=1");
                java.util.List templateList = this.dao.query(BlogTemplate.class, "updated=1");
                if (templateList != null) {
                    for (int i = 0; i < templateList.size(); i++) {
                        BlogTemplate template = (BlogTemplate) templateList.get(i);
                        String fileUrl = template.getUrl().substring(1, template.getUrl().length());
                        String zipFile = Globals.APP_BASE_DIR + fileUrl;
                        String outputDirectory = zipFile.substring(0, zipFile.lastIndexOf("/"));
                        boolean ret = new BlogFileUtil().unZip(zipFile, outputDirectory);
                        if (ret) {
                            template.setUpdated(Boolean.FALSE);
                            this.dao.update(template);
                        }
                    }
                }
                if (list != null) {
                    for (int i = 0; i < list.size(); i++) {
                        //System.out.println("生成一个文件！");
                        BlogInfo blog = (BlogInfo) list.get(i);
                        if (blog.isUserBlog()) {
                            HTMLGenerater.saveAllDocToHTML(blog);
                            HTMLGenerater.saveToHTML(blog);
                            HTMLGenerater.saveArticleIndexToHTML(blog);
                            HTMLGenerater.saveUserCircleToHTML(blog);
                        } else {
                            HTMLGenerater.saveCircleIndexToHTML(blog);
                        }
                        //TemplateGenerater.generateTemplate(blog);
                        HTMLGenerater.saveAllPhotoDocToHTML(blog);
                        HTMLGenerater.savePhotoToHTMLByBlog(blog);
                        HTMLGenerater.saveMessageToHTML(blog);
                        blog.setUpdated(Boolean.FALSE);
                        this.dao.update(blog);
                    }
                }
                /*if(circleList != null){
                for (int i = 0; i < circleList.size(); i++) {
                BlogInfo circle = (BlogInfo)circleList.get(i);
                HTMLGenerater.saveCircleIndexToHTML(circle);
                HTMLGenerater.saveMessageToHTML(circle);
                circle.setUpdated(Boolean.FALSE);
                boolean ret = this.dao.update(circle);
                }
                }*/
            } catch (Exception e) {
                logger.error("自动生成HTML错误" + e);
                e.printStackTrace();
            }
        }
    }
}
